#pragma once

typedef BOOL (WINAPI *LPLayerWindow)(HWND hWnd, COLORREF cr, BYTE bAlpha, DWORD dwFlags);

class CWindowTransparent
{
public:
	CWindowTransparent()
	{
		SetLayeredWindowAttributes = NULL;
		m_hInstUser32 = GetModuleHandle(_T("USER32.DLL"));
		SetLayeredWindowAttributes = (LPLayerWindow)GetProcAddress(m_hInstUser32, "SetLayeredWindowAttributes");
	}

	CWindowTransparent(HWND hWnd, int nTransparentPercent)
	{
		m_hInstUser32 = NULL;
		SetLayeredWindowAttributes = NULL;

		if(hWnd == NULL)
			return;

		m_hInstUser32 = GetModuleHandle(_T("USER32.DLL"));
		SetLayeredWindowAttributes = (LPLayerWindow)GetProcAddress(m_hInstUser32, "SetLayeredWindowAttributes");

		WindowTransparent(hWnd, nTransparentPercent);
	}

	~CWindowTransparent()
	{
		if(m_hInstUser32 != NULL)
			FreeLibrary(m_hInstUser32);
	}

public:
	void WindowTransparent(HWND hWnd, int nTransparentPercent)
	{
		if(SetLayeredWindowAttributes == NULL)
			return;

		DWORD dwStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
		if(!(dwStyle & WS_EX_LAYERED))
		{
			dwStyle |= WS_EX_LAYERED;
			SetWindowLong(hWnd, GWL_EXSTYLE, dwStyle);
		}

		SetLayeredWindowAttributes(hWnd, 0, (BYTE)nTransparentPercent * 255 / 100, LWA_ALPHA);
	}

protected:
	HINSTANCE	m_hInstUser32;
	LPLayerWindow SetLayeredWindowAttributes;
};